﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

/// <summary>
/// 粒子特效控制脚本
/// </summary>
public class BhvParticlesCtrl : MonoBehaviour
{
    public bool recycle = true;
    public bool FadeAnimation = false;
    public float fadeTime = 0.5f;

    /// <summary>
    /// 是否通过EffectManager 创建，如果通过EffectManager 创建，则会被EffectManager 回收
    /// </summary>
    [HideInInspector] public bool fromEffectManager = false;

    private ParticleSystem particle;

    public BhvParticlesCtrl SetLoop(bool state)
    {
        particle = GetComponent<ParticleSystem>();
        ParticleSystem.MainModule mainModule = particle.main;
        mainModule.loop = state;
        if (state == false)
        {
            mainModule.stopAction = ParticleSystemStopAction.Callback;
        }
        return this;
    }

    public BhvParticlesCtrl Reset()
    {
        particle = GetComponent<ParticleSystem>();
        ParticleSystem.MainModule mainModule = particle.main;
        particle.Play();
        return this;
    }

    /// <summary>
    /// 设置粒子是否自动回收
    /// </summary>
    public BhvParticlesCtrl SetAutoRecycle(bool auto = true)
    {
        particle = GetComponent<ParticleSystem>();
        ParticleSystem.MainModule mainModule = particle.main;
        if (auto)
        {
            mainModule.stopAction = ParticleSystemStopAction.Callback;
        }
        else
        {
            mainModule.stopAction = ParticleSystemStopAction.None;
        }
        return this;
    }

    /// <summary>
    /// 设置自动回收粒子
    /// </summary>
    public BhvParticlesCtrl SetLifeTime(float lifetime = 0)
    {
        particle = GetComponent<ParticleSystem>();
        ParticleSystem.MainModule mainModule = particle.main;
        if (lifetime == 0)
        {
            mainModule.stopAction = ParticleSystemStopAction.Callback;
        }
        else
        {
            mainModule.stopAction = ParticleSystemStopAction.None;
            StartCoroutine(DelayRecycle(lifetime));
        }
        return this;
    }

    private IEnumerator DelayRecycle(float duration)
    {
        yield return new WaitForSeconds(duration);
        OnParticleSystemStopped();
    }

    public BhvParticlesCtrl SetTransform(Vector3 localScale, Vector3 position)
    {
        transform.localScale = localScale;
        transform.position = position;
        return this;
    }

    public BhvParticlesCtrl SetTransform(Vector3 localScale, Vector3 position, Vector3 angle)
    {
        transform.localScale = localScale;
        transform.position = position;
        transform.localEulerAngles = angle;
        return this;
    }


    public void Stop()
    {
        particle?.Stop();
    }

    public void OnParticleSystemStopped()
    {
        if (!recycle) return;

        if (FadeAnimation && fadeTime > 0)
        {
            this.transform.DOScale(Vector3.zero, fadeTime).onComplete += () =>
            {
                Recycle();
            };
        }
        else
        {
            Recycle();
        }

    }

    private void Recycle()
    {
        if (fromEffectManager)
        {
            EffectManager.Instace?.PutParticles(this, this.gameObject.name);
        }
        else
        {
            var easyPool = this.GetComponentInParent<BhvEasyPool>();
            easyPool?.Put(this.gameObject);
        }
    }

}
